#include "time_24.h"

using namespace aviation;

//Constructors

Time::Time()
{
	m_minutes = 0;
	m_hours = 0;
}

Time::Time(const std::string& fcgString)
{
	//Check that the length of fcgString is 4.
	if(fcgString.length() < 4) throw std::exception("Attempted to instantiate a Time object with a string that is too short.");
	if(fcgString.length() > 4) throw std::exception("Attempted to instantiate a Time object with a string that is too long.");
	
	char n1 = fcgString[0];
	char n2 = fcgString[1];
	char n3 = fcgString[2];
	char n4 = fcgString[3];

	//Check that the input is a valid time.
	if(!(n1 >= '0' && n1 <= '2')) throw std::exception("The first digit of the time is not valid.");
	if(n1 == '2') { if(!(n2 >= '0' && n2 <= '3')) throw std::exception("The second digit of the time is not valid."); } 
	if(!(n2 >= '0' && n2 <= '9')) throw std::exception("The second digit of the time is not valid.");
	if(!(n3 >= '0' && n3 <= '5')) throw std::exception("The third digit of the time is not valid.");
	if(!(n4 >= '0' && n4 <= '9')) throw std::exception("The fourth digit of the time is not valid.");

	const int asciiOffset = 48;
	m_hours = 10*(n1-asciiOffset) + (n2-asciiOffset);
	m_minutes = 10*(n3-asciiOffset) + (n4-asciiOffset);
}

Time::Time(const Time& other)
{
	*this = other;
}

Time::~Time()
{
}

//Operators

Time& Time::operator=(const Time& other)
{
	m_minutes = other.m_minutes;
	m_hours = other.m_hours;

	return *this;
}

const bool Time::operator==(const Time& other) const
{
	return m_minutes == other.m_minutes && m_hours == other.m_hours;
}

const bool Time::operator!=(const Time& other) const
{
	return !(*this == other);
}

const bool Time::operator<(const Time& other) const
{
	return m_hours < other.m_hours
		|| m_hours == other.m_hours && m_minutes < other.m_minutes;
}

//Accessors

const unsigned int Time::GetMinutes() const
{
	return m_minutes;
}

const unsigned int Time::GetHours() const
{
	return m_hours;
}

const std::string Time::AsString() const
{
	String s1 = util::XtoA(m_hours, 2, '0');
	String s2 = util::XtoA(m_minutes, 2, '0');
	return (s1+s2);
}

